package com.fortune.service.impl;

import com.fortune.constants.enums.ProductStock;
import com.fortune.dao.MProductStockDetailInfoDao;
import com.fortune.dao.MStoreDeficitDao;
import com.fortune.dao.MStoreInDao;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitAddDto;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitEditDto;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitInfoDto;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitListDto;
import com.fortune.dto.in.mStoreIn.MStoreInAddDto;
import com.fortune.dto.in.mStoreIn.MStoreInEditDto;
import com.fortune.dto.in.mStoreIn.MStoreInListDto;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.MStoreDeficitService;
import com.fortune.service.MStoreInService;
import com.fortune.vo.mStoreDeficit.MStoreDeficitInfo;
import com.fortune.vo.mStoreDeficit.MStoreDeficitInfoDetail;
import com.fortune.vo.mStoreIn.MStoreInInfo;
import com.fortune.vo.productStockDetailInfo.ProductStockDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class MStoreDeficitServiceImpl implements MStoreDeficitService {
    @Autowired
    private MStoreDeficitDao mStoreDeficitDao;


    @Override
    @Transactional
    public int addDeficitBase(String companyId,String uid, MStoreDeficitAddDto addDto)throws InsertInnerErrorException {
        String uuid= UUID.randomUUID().toString().replace("-", "");
        int addDeficitBase=mStoreDeficitDao.addDeficitBase(companyId,uuid,uid,addDto);
        if(addDeficitBase>0){
            int addDeficitDetail=0;
            for(MStoreDeficitInfoDto infoDto: addDto.getmStoreDeficitInfoDto()){
                addDeficitDetail=mStoreDeficitDao.addDeficitDetail(companyId,uuid,infoDto);
                if(addDeficitDetail<1){
                    throw new InsertInnerErrorException("新增报亏商品失败");
                }
            }
            return addDeficitDetail;

        }
        return addDeficitBase;
    }

    @Override
    public List<MStoreDeficitInfo> findDeficitList(String companyId, MStoreDeficitListDto mStoreDeficitListDto) {
        return  mStoreDeficitDao.findDeficitList(companyId,mStoreDeficitListDto);
    }

    @Override
    public int findDeficitListCount(String companyId, MStoreDeficitListDto mStoreDeficitListDto) {
        return  mStoreDeficitDao.findDeficitListCount(companyId,mStoreDeficitListDto);
    }

    @Override
    public MStoreDeficitInfoDetail findDetail(String companyId, String deficitId) {
        return mStoreDeficitDao.findDeficitInfo(companyId,deficitId);
    }

    @Override
    public int editMStoreDeficitConfirm(String companyId, String uuid,String operatorId) {
        return mStoreDeficitDao.editMStoreDeficitConfirm(companyId, uuid,operatorId);
    }

    @Override
    public int deleteMStoreDeficit(String companyId, String uuid) {
        return mStoreDeficitDao.deleteMStoreDeficit(companyId, uuid);
    }

    @Override
    public int editDeficitBase(String companyId, MStoreDeficitEditDto editDto) {
        int editDeficitBase=mStoreDeficitDao.editDeficitBase(companyId,editDto);
        if(editDeficitBase>0){
            String uuid=editDto.getUuid();
            int addDeficitDetail=0;
            int deleteInfo=mStoreDeficitDao.deleteInfo(companyId,uuid);
            if(deleteInfo>0){
                for(MStoreDeficitInfoDto infoDto: editDto.getmStoreDeficitInfoDto()){
                    addDeficitDetail=mStoreDeficitDao.addDeficitDetail(companyId,uuid,infoDto);
                    if(addDeficitDetail<1){
                        throw new InsertInnerErrorException("编辑报亏商品失败");
                    }
                }
            }
            return addDeficitDetail;
        }
        return editDeficitBase;
    }
}
